<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <!-- 用户id、ip、登录时间、地区、状态 -->
            <a-col :md="8" :sm="24">
              <a-form-item label="ID">
                <a-input v-model="queryParam.id" placeholder="" />
              </a-form-item>
            </a-col>
            <!-- <a-col :md="8" :sm="24">
              <a-form-item label="状态">
                <a-select v-model="queryParam.status" placeholder="请选择">
                  <a-select-option v-for="it in statusList" :key="it.id" :value="it.id">{{ it.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="联系人">
                  <a-input v-model="queryParam.contactName" placeholder="" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="联系人号码">
                  <a-input v-model="queryParam.contactPhone" placeholder="" />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
            <!-- <a-col :md="8" :sm="24"> -->
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (this.queryParam = {})">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd" v-action:tb_add>新建</a-button>
      </div>
      <s-table ref="table" size="default" rowKey="id"  :columns="columns" :data="loadData" showPagination="auto" :scroll="{ x: 'auto' }">
        <span slot="param" slot-scope="text">
          <div class="param-wrap">{{ text }}</div>
        </span>
        <span slot="type" slot-scope="text">
          <a-tag :color="text | statusFilter(typeList, 'status')">{{ text | statusFilter(typeList, 'name') }}</a-tag>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record)" v-action:tb_update>编辑</a>
            <a-divider type="vertical" />
            <a @click="handleDel(record)" v-action:tb_update>删除</a>
          </template>
        </span>
      </s-table>
      <TB-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        :typeList="typeList"
        @cancel="handleCancel"
        @ok="handleOk"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { financeTBList,
financeTBAdd,
financeTBUpdate } from '@/api/system'
import { eachMap, needList, financeStatusList, statusList } from '@/utils/other'
// import StepByStepModal from './modules/StepByStepModal'
import TBForm from './modules/TBForm'
const columns = [
  // id、类型、名称、描述、添加人、添加时间、最后编辑人、最后编辑时间
  {
    title: 'ID',
    dataIndex: 'id',
    width:'100px'
  },
  {
    title: '类型',
    dataIndex: 'type',
    scopedSlots: { customRender: 'type' },
  },
  {
    title: '名称',
    dataIndex: 'name',
    width:'120px',
    // sorter: true,
  },
  {
    title: '联系人',
    dataIndex: 'contactName',
    width:'100px',
    // sorter: true,
  },
  {
    title: '联系号码',
    dataIndex: 'contactPhone',
    width:'150px',
    // sorter: true,
  },
  {
    title: '项目数量',
    dataIndex: 'projectNum',
    width:'120px',
    sorter: true,
  },
  
  {
    title: '备注',
    dataIndex: 'remark',
    width:'120px',
    // sorter: true,
  },
  {
    title: '添加人',
    dataIndex: 'addUserName',
    width:'100px',
    customRender: (text) => text || '--',
  },
  {
    title: '添加时间',
    dataIndex: 'addTime',
    width:'180px',
    customRender: (text) => text || '--',
  },
  {
    title: '最后编辑人',
    dataIndex: 'lastUpdateUserName',
    width:'120px',
    customRender: (text) => text || '--',
  },
  {
    title: '最后编辑时间',
    dataIndex: 'lastUpdateTime',
    width:'180px',
    customRender: (text) => text || '--',
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' },
  },
]
const typeList = [
  {
    id: 1,
    name: '公司',
    status: 'green',
  },
  {
    id: 2,
    name: '个人',
    status: 'blue',
  },
]

export default {
  name: 'AWM-TB',
  components: {
    STable,
    Ellipsis,
    TBForm,
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      parameter: {
        pageNum: 1,
        pageSize: 10,
      },
      queryParam: {},
      data: [],
      typeList,

      // 排序
      sortJ: {
        projectNum: 'project_num',
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        let sortJ = this.sortJ
        if (parameter.sortField) {
          parameter.sortJ = sortJ
        }
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return financeTBList(requestParameters).then((res) => {
          res.data.data.map(v=>{v.children = v.childTypeList;delete v.childTypeList; return v})
          this.financeTypeList = res.data.data
          let result = {
            data: res.data.data,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalCount: res.data.total,
            totalPage: res.data.pages,
          }
          return result
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
    }
  },
  created() {},
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange,
      }
    },
  },
  methods: {
    handleAdd() {
      this.mdl = null
      this.visible = true
    },
    handleEdit(record) {
      this.visible = true
      this.mdl = { ...record }
    },
    saveCB() {
      const form = this.$refs.createModal.form
      this.visible = false
      this.confirmLoading = false
      // 重置表单数据
      form.resetFields()
      // 刷新表格
      this.$refs.table.refresh()
    },
    handleOk() {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          needList.forEach((v) => {
            values[v.value] = 0
          })
          if(values.data){
            values.data.forEach((v) => {
              values[v] = 1
            })
          }
          
          delete values.data
          // return false
          if (values.id > 0) {
            financeTBUpdate(values)
              .then((res) => {
                this.saveCB()
                this.$message.success('修改成功!')
              })
              .catch((err) => {
                this.saveCB()
              })
          } else {
            delete values.id
            financeTBAdd(values)
              .then((res) => {
                this.saveCB()
                this.$message.success('新增成功!')
              })
              .catch((err) => {
                this.saveCB()
              })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date()),
      }
    },
    handleDel(a_record) {
      let self = this
      const modal = self.$confirm({
        title: '商务删除',
        content: `是否删除商务:${a_record.name}`,
        okText: '确认',
        cancelText: '取消',
        onOk: () => self.handleDelete(a_record.id, modal),
        onCancel: () => {},
      })
    },
    
    handleDelete(a_id, a_modal) {
      return new Promise((resolve, reject) => {
        let data = { id: a_id, status: -1 }
        financeTBUpdate(data)
          .then((res) => {
            this.$refs.table.refresh()
            this.$message.success('删除成功!')
            resolve()
          })
          .catch((err) => {
            a_modal.destroy()
            this.$message.error('操作失败!')
          })
      })
    },
  },
}
</script>
<style lang="less" scoped>
.param-wrap {
  min-width: 200px;
  max-width: 300px;
}
/deep/ tr.ant-table-row-level-1{
  background-color: #f8f8f8 !important;
}
</style>


 